/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.instruction;

import java.io.IOException;

import javax.servlet.ServletException;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.RequestPath;

/**
 * 
 * @author zhiliang.wang
 *
 */
public class RedirectInstruction extends AbstractInstruction {


	@Override
	public void doRender(Invocation invocation) throws IOException {
		String location = resolvePlaceHolder(location(), invocation);
		invocation.getResponse().sendRedirect(location);
	}
	
	// ----------------------------------------
	
	private String location;

	public RedirectInstruction module(final String module) {
		this.preInstruction = new Instruction() {
			@Override
			public void render(Invocation invocation) throws IOException,
					ServletException, Exception {
				String ctxpath = invocation.getRequestPath().getCtxpath();
				if (module.length() == 0) {
					if (ctxpath.length() == 0) {
						location("/");
					} else {
						location(ctxpath);
					}
				} else if (module.charAt(0) != '/') {
					location(ctxpath + "/" + module);
				} else {
					location(ctxpath + module);
				}
			}
		};
		return this;
	}

	public RedirectInstruction controller(final String controller) {
		this.preInstruction = new Instruction() {
			@Override
			public void render(Invocation invocation) throws IOException,
					ServletException, Exception {
				String controllerPath = controller;
				if (controller.length() > 0 && controller.charAt(0) != '/') {
					controllerPath = "/" + controller;
				}
				RequestPath requestPath = invocation.getRequestPath();
				location(requestPath.getCtxpath() + requestPath.getModulePath() + controllerPath);
			}
		};
		return this;
	}

	public RedirectInstruction action(final String action) {
		this.preInstruction = new Instruction() {
			@Override
			public void render(Invocation invocation) throws IOException,
					ServletException, Exception {
				String actionPath = action;
				if (action.length() > 0 && action.charAt(0) != '/') {
					actionPath = "/" + action;
				}
				RequestPath requestPath = invocation.getRequestPath();
				location(requestPath.getCtxpath() + requestPath.getModulePath() 
						+ requestPath.getControllerPath() + actionPath);
			}
		};
		return this;
	}

	public String location() {
		return location;
	}

	public RedirectInstruction location(String location) {
		this.location = location;
		return this;
	}
}
